﻿using System;
using System.Timers;
using System.Net;
using System.Net.Sockets;
using SuperSocket.WebSocket;

public static class Tool
{
    /// <summary>
    /// 数据文件路径
    /// </summary>
    public readonly static string DATA_FILE_PATH = Environment.CurrentDirectory + @"\user.data";

    #region 帮助方法

    /// <summary>
    /// 窗口打印日志
    /// </summary>
    /// <param name="log">日志内容</param>
    /// <param name="isRecord">是否记录</param>
    public static void Log(string log, bool isRecord = false)
    {
        MyEventManager.Instance.Broadcast(MyEvent.日志, log, isRecord);
    }

    /// <summary>
    /// 获得本机IP地址
    /// </summary>
    /// <returns>本机IP地址</returns>
    public static string GetLocalIP()
    {
        try
        {
            string hostName = Dns.GetHostName();
            IPHostEntry IpEntry = Dns.GetHostEntry(hostName);
            for (int i = 0; i < IpEntry.AddressList.Length; i++)
            {
                //从IP地址列表中筛选出IPv4类型的IP地址
                //AddressFamily.InterNetwork表示此IP为IPv4,
                //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                {
                    return IpEntry.AddressList[i].ToString();
                }
            }
            return string.Empty;
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("获取本机IP出错:" + ex.Message);
            return string.Empty;
        }
    }

    #endregion

    #region 静态方法

    /// <summary>
    /// 发送协议
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    /// <param name="session">客户端会话</param>
    /// <param name="type">协议类型</param>
    /// <param name="t">协议数据</param>
    /// <param name="log">是否打印日志</param>
    public static void Send<T>(this WebSocketSession session, MsgType type, T t = null, bool log = true) where T : class
    {
        byte[] data = Common.ComposeData(type, t);
        session.Send(data, 0, data.Length);

        if (log)
            Log(Common.FormatSocket(true, type, data, t.ToString()));
    }

    /// <summary>
    /// 启动计时器
    /// </summary>
    /// <param name="callback">计时回调</param>
    /// <param name="time">间隔时间（毫秒）</param>
    /// <param name="autoReset">是否循环</param>
    /// <returns>计时器</returns>
    public static Timer StartTimer(this Timer timer, ElapsedEventHandler callback, float time, bool autoReset = true)
    {
        timer = new Timer(time);
        timer.Elapsed += callback;
        timer.AutoReset = autoReset;
        timer.Enabled = true;
        return timer;
    }

    /// <summary>
    /// 关闭计时器
    /// </summary>
    /// <param name="timer">计时器</param>
    /// <param name="callback">计时器回调</param>
    public static void CloseTimer(this Timer timer, ElapsedEventHandler callback)
    {
        if (timer == null)
            return;

        timer.Elapsed -= callback;
        timer.Stop();
        timer.Close();
        timer = null;
    }

    #endregion
}